/**********************************************************************
Compile : gcc -O3 -o generate1 generate.c main1.c
Usage   : ./generate1 mode1 mode2 N ratio1/NL ratio2 M seed
mode1   : 0 disallow self-activation; 1 allow self-activation
mode2   : Additional Requirements: 0 no requirements; 1 restriction I; 2 restriction II; 3 restriction III
N       : number of the nodes
ratio1  : number of links / (N * N)   when <= 1.0
NL      : number of links per node    when >= 2.0
ratio2  : number of red links / number of green links
M       : the number of the attempts
seed    : random seed
***********************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include "generate.h"

int main (int argc, char *argv[])
{
	int N, M, mode1, mode2;
	float ratio, ratio2;
	long int seed;

	mode1 = atoi (argv[1]);
	mode2 = atoi (argv[2]);
	N = atoi (argv[3]);
	ratio = atof (argv[4]);
	if (ratio >= 2.0)
		ratio = ratio / (float) N;
	ratio2 = atof (argv[5]);
	if (mode1 == 1)
		ratio2 = (ratio * ratio2 / (ratio2 + 1.0) - 1.0 / N) / (ratio / (ratio2 + 1.0) - 1.0 / N);
	else
		ratio2 = (ratio * ratio2 / (ratio2 + 1.0) - (1.0 + (ratio * N - 2.0) / (N - 1.0))/ N) / (ratio / (ratio2 + 1.0) - 1.0 / N);

	ratio = (ratio * N - 2.0) / (N - 2.0);
	M = atoi (argv[6]);
	seed = atoi (argv[7]);
	srand48 (seed);

	int i, j, k, l, p, x, y, n, nR, nG;;
	int network[N][N];

	n = 1;

	for (i = 0; i < N; i++)
	{
		n *= 2;
	}

	struct state state[n];
	int pathway[n][N];

	init_state (state, n, N);

	for (i = 0; i < M; i++)
	{
		generate_network (N, 0, mode1, network, ratio, ratio2, &nR, &nG);

		for (j = 0; j < n; j++)
		{
			state[j].status2 = dynamics (N, state[j].status, network);
		}

		for (j = 0; j < n; j++)
		{
			state[j].stable = check_stable (N, j, state, network, mode2);
			state[j].pnflag = 0;
		}

		check_final (N, n, state);

		int max_l1, max_l11, max_l2, max_l21, p1, p11, p2, p21;

		max_l1 = max_l11 = max_l2 = max_l21 = -1;
		for (j = 0; j < n; j++)
		{
			if (state[j].flag == 0 || state[j].flag == 1)
			{
				get_process (N, state[j].length, pathway, state, j);
				if (state[j].length > 1 && check_process (N, state[j].length, pathway) == 1)
				{
					if (max_l1 == -1 || max_l1 < state[j].length)
					{
						max_l1 = state[j].length;
						p1 = j;
					}
				}
				else
				{
					if (max_l11 == -1 || max_l11 < state[j].length)
					{
						max_l11 = state[j].length;
						p11 = j;
					}
				}
			}

			if (state[j].flag == 3)
			{
				get_process (N, state[j].length, pathway, state, j);
				if (check_process (N, state[j].length, pathway) == 1)
				{
					if (max_l2 == -1 || max_l2 < state[j].length)
					{
						max_l2 = state[j].length;
						p2 = j;
					}
				}
				else
				{
					if (max_l21 == -1 || max_l21 < state[j].length)
					{
						max_l21 = state[j].length;
						p21 = j;
					}
				}
			}
		}

		printf ("%2d %2d %2d %2d %2d %2d %2d\n", nG + nR, nG, nR, max_l1, max_l11, max_l2, max_l21);
	}
}
